{***************************************************************
    SpyGestion  Copyright (C) 2008 Christophe KOALI <nature2gas@gmail.com>
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
****************************************************************}
unit frmOGSpyExport;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,

  ESBDates,
  modOGSSupport,
  modPlayerFinal,

     modGalaxiePlanetes,
     modBaseDeDonnesRapport,

   Spin;

type
  TformOGSExport = class(TForm)
    GroupBox1: TGroupBox;
    txtURL: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    txtUsername: TEdit;
    txtPassword: TEdit;
    Label3: TLabel;
    txtDebug: TMemo;
    cmdRapportsExport: TButton;
    GroupBox2: TGroupBox;
    seItemsParInjection: TSpinEdit;
    seDuration: TSpinEdit;
    cmdGalaxyExport: TButton;
    cmdLogin: TButton;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure cmdLoginClick(Sender: TObject);
    procedure cmdGalaxyExportClick(Sender: TObject);
    procedure cmdRapportsExportClick(Sender: TObject);
    procedure txtURLExit(Sender: TObject);
  private
    { Private declarations }
    FOGS: TOGSMotor;

    FCurrentPlayer: TPlayerFinal;

    FCurrDBGalaxy  : TCollectionGalaxiePlanete;
    FCurrDBRapports: TCollectionRapportHead;

  public
    { Public declarations }
    procedure DoShow(Player: TPlayerFinal; dbGalaxy: TCollectionGalaxiePlanete; dbRapports: TCollectionRapportHead);

    procedure Status(const msg: string);

  end;

var
  formOGSExport: TformOGSExport;

implementation


uses gnugettext;


{$R *.dfm}

procedure TformOGSExport.Status(const msg: string);
begin
   txtDebug.Lines.Add(Msg);
end;


procedure TformOGSExport.txtURLExit(Sender: TObject);
var xPos: integer;
    s: string;
begin
    xPos := pos('.php', txtURL.Text);
    if xPos = 0 then begin
       xPos := pos('/index.', txtURL.Text);
    end;

    if xPos > 0 then begin
       xPos := LastDelimiter('/', txtURL.Text);
       if xPos = 0 then begin
          exit;
       end;

       txtURL.Text := copy(txtURL.Text, 0, xPos);
       exit;
    end;

    s := txtURL.Text;
    if length(s) < 5 then begin
       exit;
    end;

    if s[length(s)] <> '/' then begin
       s := s + '/';
       txtURL.Text := s;
    end;




end;

procedure TformOGSExport.cmdGalaxyExportClick(Sender: TObject);
var dateLimit: int64;
begin

   cmdGalaxyExport.Enabled := false;

   if FOGS.IsLogged = false then begin
      // pas loggé
      cmdGalaxyExport.Enabled := true;
      exit;
   end;

   dateLimit := DateTimeToUnix(Now);
   dateLimit := dateLimit - seDuration.Value;

   FOGS.QuantiteParRequete := seItemsParInjection.Value;
   FOGS.ExportCartographie(FCurrDBGalaxy, dateLimit);

   cmdGalaxyExport.Enabled := true;

end;

procedure TformOGSExport.cmdLoginClick(Sender: TObject);
begin

   cmdLogin.Enabled := false;

   FOGS.Username := txtUsername.Text;
   FOGS.Password := txtPassword.Text;
   FOGS.QuantiteParRequete := 5;

   FOGS.DebugProc := Status;

   if FOGS.Login(txtURL.Text) > 0 then begin
      Status('Login réussi');
      
      FCurrentPlayer.Conf.OGSServeurURL := txtURL.Text;
      FCurrentPlayer.Conf.OGSUsername   := txtUsername.Text;
      FCurrentPlayer.Conf.OGSPassword   := txtPassword.Text;

      cmdGalaxyExport.Enabled := true;
   end;


   cmdLogin.Enabled := true;
end;

procedure TformOGSExport.cmdRapportsExportClick(Sender: TObject);
var dateLimit: int64;
begin
     cmdRapportsExport.Enabled := false;

     dateLimit := DateTimeToUnix(Now);
     dateLimit := dateLimit - seDuration.Value;

     FOGS.QuantiteParRequete := seItemsParInjection.Value;
     FOGS.ExportRapports(FCurrDBRapports, dateLimit);

     
     cmdRapportsExport.Enabled := true;
end;

procedure TformOGSExport.DoShow(Player: TPlayerFinal; dbGalaxy: TCollectionGalaxiePlanete; dbRapports: TCollectionRapportHead);
begin
    FCurrDBGalaxy   := dbGalaxy;
    FCurrDBRapports := dbRapports;

    FCurrentPlayer := Player;

    txtURL.Text      := FCurrentPlayer.Conf.OGSServeurURL;
    txtUsername.Text := FCurrentPlayer.Conf.OGSUsername;
    txtPassword.Text := FCurrentPlayer.Conf.OGSPassword;

    Show;
end;


procedure TformOGSExport.FormCreate(Sender: TObject);
begin
    FOGS := TOGSMotor.Create;

    // traduction, car la form est créé sur le vol
    TranslateComponent(Self);

end;

procedure TformOGSExport.FormDestroy(Sender: TObject);
begin
   FOGS.Free;
end;

end.
